package com.ruoyi.yx.controller;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.YxAndroidApplicationVo;
import com.ruoyi.common.core.domain.entity.YxDeviceNoticeMessageVo;
import com.ruoyi.framework.web.domain.AppInfoVo;
import com.ruoyi.framework.web.domain.AppUsageInfoVo;
import com.ruoyi.framework.web.service.*;
import com.ruoyi.framework.web.domain.AndroidDeviceVo;
import com.ruoyi.system.domain.YxDeviceLocation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@Api("设备信息表")
@RequestMapping("/api/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private AppInfoService appInfoService;

    @Autowired
    private ControlService controlService;

    @Autowired
    private LocationService locationService;

    @Autowired
    private DeviceLogUploadRecordService deviceLogUploadRecordService;

    @Autowired
    private DeviceNoticeMessageService deviceNoticeMessageService;

    /**
     * 通过设备id登录
     * @param androidDeviceVo
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    @ApiOperation("通过设备id登录")
    public R<String> login(@RequestBody AndroidDeviceVo androidDeviceVo)throws Exception{
        return deviceService.login(androidDeviceVo);
    }

    /**
     * 设备注册
     * @param androidDeviceVo
     * @return
     * @throws Exception
     */
    @PostMapping("/register")
    @ApiOperation("设备注册")
    public R<String> register(@RequestBody AndroidDeviceVo androidDeviceVo)throws Exception{
        return deviceService.register(androidDeviceVo);
    }

    /**
     * 获取 临时uuid 去绑定设备
     * @return
     * @throws Exception
     */
    @PostMapping("/getUuui2BindUser")
    @ApiOperation("获取 临时uuid 去绑定设备")
    public R<String> getUuui2BindUser()throws Exception{

        return deviceService.getUuui2BindUser();
    }

    /**
     * 绑定设备 cid
     * @return
     * @throws Exception
     */
    @PostMapping("/bindCid")
    @ApiOperation("获取 临时uuid 去绑定设备")
    public R<String> bindCid(@RequestBody Map<String, String> json)throws Exception{

        return deviceService.bindCid(json.get("cid"));
    }

    /**
     * 上传设备应用信息
     * @return
     * @throws Exception
     */
    @PostMapping("/upLoadAppInfo")
    @ApiOperation("上传设备应用信息")
    public R<List<String>> upLoadAppInfo(@RequestBody List<AppInfoVo> appInfoList)throws Exception{

        return appInfoService.upLoadAppInfo(appInfoList);
    }

    /**
     * 获取授权应用列表
     * @return
     * @throws Exception
     */
    @PostMapping("/app/authorizeList")
    @ApiOperation("获取授权应用列表")
    public R<List<YxAndroidApplicationVo>> appAuthorizeList()throws Exception{
        return appInfoService.deviceAppAuthorizeList();
    }

    /**
     * 更新应用图标
     * @return
     * @throws Exception
     */
    @PostMapping("/updateAppIcon")
    @ApiOperation("更新应用图标")
    public R<String> updateAppIcon(@RequestBody AppInfoVo appInfoVo)throws Exception{

        return appInfoService.upLoadAppInfo(appInfoVo);
    }

    /**
     * 更新应用图标
     * @return
     * @throws Exception
     */
    @PostMapping("/updateAppIconList")
    @ApiOperation("更新应用图标")
    public R<String> updateAppIcon(@RequestBody Map<String,String> packageIconMap)throws Exception{

        return appInfoService.upLoadAppInfoList(packageIconMap);
    }



    /**
     * 上传设备应用使用时长信息
     * @return
     * @throws Exception
     */
    @PostMapping("/upLoadAppUsageInfo")
    @ApiOperation("上传设备应用使用时长信息")
    public R<String> upLoadAppUsageInfo(@RequestBody List<AppUsageInfoVo> appUsageInfoVoList)throws Exception{

        return appInfoService.upLoadAppUsageInfo(appUsageInfoVoList);
    }


    /**
     * 截图回传接口
     * @return
     * @throws Exception
     */
    @PostMapping("/captureDesktopBack")
    @ApiOperation("截图回传接口")
    public R<String> captureDesktopBack(@RequestBody Map<String,String> json)throws Exception{

        return controlService.captureDesktopBack(json.get("taskId"),json.get("url"));
    }


    /**
     * 上报设备位置信息
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadLocation")
    @ApiOperation("上报设备位置信息")
    public R<String> uploadLocation(@RequestBody YxDeviceLocation yxDeviceLocation)throws Exception{

        return locationService.uploadLocation(yxDeviceLocation);
    }


    /**
     *  锁屏上报接口
     * @return
     * @throws Exception
     */
    @PostMapping("/taskBack")
    @ApiOperation("锁屏上报接口")
    public R<String> taskBack(@RequestBody Map<String,String> json)throws Exception{

        return controlService.taskBack(json.get("taskId"),json.get("data"));
    }

    /**
     *  锁屏上报接口
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadLogZip")
    @ApiOperation("锁屏上报接口")
    public R<String> uploadLogZip(@RequestBody Map<String,String> json)throws Exception{

        return deviceLogUploadRecordService.uploadLogZip(json.get("taskId"),json.get("data"));
    }


    /**
     *  上报信息
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadNoticeDeviceMessage")
    @ApiOperation("锁屏上报接口")
    public R<String> uploadNoticeDeviceMessage(@RequestBody Map<String,String> json)throws Exception{

        return deviceNoticeMessageService.uploadNoticeDeviceMessage(json.get("packageName"),json.get("content"));
    }


    /**
     *  批量上报信息
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadNoticeDeviceMessageList")
    @ApiOperation("批量上报信息")
    public R<String> uploadNoticeDeviceMessageList(@RequestBody List<YxDeviceNoticeMessageVo> list)throws Exception{

        return deviceNoticeMessageService.uploadNoticeDeviceMessageList(list);
    }

    /**
     * 查询设备锁屏状态
     * @return
     * @throws Exception
     */
    @PostMapping("/lock/status")
    @ApiOperation("查询设备锁屏状态")
    public R<Map> appUsageList()throws Exception{
        return controlService.lockStatus();
    }
}
